package ee.falcon.frame.gui;

import java.awt.Color;
import java.awt.Font;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableCellRenderer;



/**
 * A utility(library) class encapsulating massive or commonly used (tedious) GUI functions into comfortably sized
 * static methods that can be used from everywhere.
 *
 * @author Priidu Neemre
 */
public class GUIUtil {
       
        /**
         * Aligns the contents in all the cells of a JTable according to a provided position/direction.
         *
         * @param direction     the new direction the contents of the provided JTable should follow
         * @param table the JTable having its contents direction modified
         */
        public static void alignJTableContents(int direction, JTable table){
               
                DefaultTableCellRenderer center = new DefaultTableCellRenderer();
                center.setHorizontalAlignment(direction);
                       
                for(int i = 0; i < table.getColumnModel().getColumnCount(); i++){
                        table.getColumnModel().getColumn(i).setCellRenderer(center);
                }
        }
       
        /**
         * Sets a new width (provided) for all the columns in a JTable.
         *
         * @param table the JTable having its column size modified
         * @param width the new width of the columns
         */
        public static void setAllJTableColumnWidths(JTable table, int width){
                for(int i = 0; i < table.getColumnModel().getColumnCount(); i++){
                        table.getColumnModel().getColumn(i).setPreferredWidth(width);
                }
        }
       
        /**
         * Sets a new width (provided) for a single column in a JTable.
         *
         * @param table the JTable having its column size modified
         * @param width the new width of the column
         * @param index the index of the column (in the JTable)
         */
        public static void setJTableColumnWidth(JTable table, int width, int index){
                table.getColumnModel().getColumn(index).setPreferredWidth(width);
        }
       
        /**
         * Applies the instruction frames' theme to a <code>JLabel</code> specified by parameter.
         *
         * @param temp          the <code>JLabel</code> the theme is applied to
         * @param border        the border being added to the <code>JLabel</code>
         * @return a <code>JLabel</code> tuned to the overall graphical feel of the frame
         */
        public static JLabel applyJLabelStyle(JLabel temp, EmptyBorder border, Font applyableFont){
                temp.setFont(applyableFont);
                temp.setBorder(border);
                temp.setForeground(Color.WHITE);
                return temp;
        }
       
        /**
         * Creates a <code>JPanel</code> to surrond another <code>JComponent</code> specified by parameter.
         *
         * @param component             the <code>JComponent</code> to be surrounded
         * @param axis                  the axis type of the <code>JPanel</code>'s <code>BoxLayout</code> layout
         * @param sequence              an integer specifying the order in which the components should be inserted into the <code>JPanel</code>
         * @return                              the desired <code>JPanel</code> containing the component provided plus some HorizontalGlue
         */
        public static JPanel createTextContainer(JComponent component, int axis, int sequence){
                JPanel temp = new JPanel();
                temp.setLayout(new BoxLayout(temp, axis));
                if(sequence == 0){
                        temp.add(component);
                        temp.add(Box.createHorizontalGlue());
                }else if(sequence == 1){
                        temp.add(Box.createHorizontalGlue());
                        temp.add(component);
                }
                temp.setOpaque(false);
               
                return temp;
        }
       
        /**
         * Attempts to set the Look and Feel of the GUI to the provided theme. If setting the Look and Feel
         * fails, an error is displayed and the default (Metal) Look and Feel is used instead.
         *
         * @param string        the name of the Look and Feel (as a <code>String</code>)
         */
        public static void setLookAndFeel(String string) {
                try {
                    for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
                        //System.out.println(info.getName());   //DEBUG INFO
                        if (string.equals(info.getName())) {
                            UIManager.setLookAndFeel(info.getClassName());
                            break;
                        }
                    }
                } catch (Exception e) {
             //           ErrorPumper.errorMsg(ErrorPumper.ERR001_FAIL_LOAD_LOOKANDFEEL, e);
                }
        }
}
